package com.zklh.leetcode.lcp;

import java.util.ArrayList;
import java.util.List;

/**
 * 汇总区间算法实现
 * @version 1.0
 * @Author tongnan
 * @Date 2023/9/15
 */
public class Solution228 {
    public List<String> summaryRanges(int[] nums) {
        int start = 0;
        int end = 0;
        List<String> result = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        while (end < nums.length) {
            while (end + 1 < nums.length && nums[end] == nums[end+1] - 1) end++;
            sb.delete(0, sb.capacity());
            sb.append(nums[start]);
            if (end > start) {
                sb.append("->").append(nums[end]);
            }
            end++;
            start = end;
            result.add(sb.toString());
        }
        return result;
    }
}